<template>
  <div>
    <!-- 评论列表 -->
    <div class="cmt-list">
      <!-- 评论的 Item 项 -->

      <van-list
        class="cmt-list"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多评论了"
        :immediate-check="false"
        @load="onLoad"
      >
        <div class="cmt-item" v-for="item in cmtList" :key="item.id">
          <!-- 头部区域 -->
          <div class="cmt-header">
            <!-- 头部左侧 -->
            <div class="cmt-header-left">
              <img src alt class="avatar" />
              <span class="uname">{{ item.aut_name }}</span>
            </div>
            <!-- 头部右侧 -->
            <div class="cmt-header-right">
              <van-icon
                name="like"
                size="16"
                color="red"
                @click="delLike(item)"
                v-if="item.is_liking"
              />
              <van-icon name="like-o" size="16" color="gray" @click="addLike(item)" v-else />
            </div>
          </div>
          <!-- 主体区域 -->
          <div class="cmt-body">{{ item.content }}</div>
          <!-- 尾部区域 -->
          <div class="cmt-footer">{{ item.pubdate | dateFormat }}</div>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getCmtListAPI, addLikeCmtAPI, delLikeCmtAPI } from '../../api/articleAPI'
export default {
  name: 'ArtCmt',
  props: {
    artId: {
      type: [Number, String],
      required: true
    }
  },
  data () {
    return {
      cmtList: [],
      offset: null,
      loading: false,
      finished: false
    }
  },
  methods: {
    async initCmtList () {
      const { data: res } = await getCmtListAPI(this.artId, this.offset)
      if (res.message === 'OK') {
        // console.log('搜索结果:', res)
        this.cmtList = [...this.cmtList, ...res.data.results]
        // console.log('这是请求的结果', this.searchList)
        this.offset = res.data.last_id

        this.loading = false

        if (res.data.results.length === 0) {
          this.finished = true
        }
      }
    },
    onLoad () {
      this.initCmtList()
    },
    async addLike (item) {
      const { data: res } = await addLikeCmtAPI(item.com_id)
      if (res.message === 'OK') {
        // console.log(res)
        // 2. 在客户端，将点赞的状态设置为 true
        item.is_liking = true
      }
    },
    async delLike (item) {
      const { data: res } = await delLikeCmtAPI(item.com_id)
      console.log(res)
      if (res === '') {
        // 2.1 提示用户
        this.$toast.success('取消点赞成功！')
        // 2.2 手动更改关注的状态
        item.is_liking = false
      }
    }
  },
  created () {
    this.initCmtList()
  }
}
</script>

<style lang="less" scoped>
.cmt-list {
  padding: 10px;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}
</style>
